import { useGlobalStore } from "@/stores";
import Taro from "@tarojs/taro";

export interface Options {
    method : 'POST' | 'GET' | 'DELETE';
    data? : any;
}

interface ApiResult<T = any> {
    success : boolean;
    code? : string | number;
    msg : string;
    data? : T;
}


class Http {
    private sending:boolean = false;

    async post<T = any>(url: string, data?: any){
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.post<T>(url, data)
        this.sending = false
        return result
    }

    static post<T = any>(url: string, data?: any) {
        let options:Options = {
            method : 'POST'
        }
        if(data) {
            options.data = data
        }

        return Http.send<T>(url, options)
    }

    async get<T = any>(url: string, data?: any){
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.get<T>(url, data)
        this.sending = false
        return result
    }

    static get<T = any>(url: string, data?: any){
        let options:Options = {
            method : 'GET'
        }

        if(data) {
            options.data = data
        }

        return Http.send<T>(url, options)
    }

    async delete<T = any>(url: string, data?: any){
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.delete<T>(url, data)
        this.sending = false
        return result
    }

    static delete<T = any>(url: string, data?: any){
        let options:Options = {
            method : 'DELETE'
        }

        if(data) {
            options.data = data
        }

        return Http.send<T>(url, options)
    }

    static async uploadFile<T = any>(url:string, file:{name:string, path : string}, formData?:any) {
        const global = useGlobalStore()


        let result:ApiResult<T> = {
            msg : '',
            success : false
        }

        try {
            let header:{[key : string] : string | number} = {
                'Content-type' : 'application/json',
                'client-type' : global.client
            }

            if(global.accessToken) {
                header.Authorization = global.accessToken//'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOjEzMDEzNDAzLCJpZCI6NjQ1NjcwMSwidGltZSI6MTY5ODExMzE5OSwiaWF0IjoxNjk4MTEzMTk5LCJleHAiOjE3MDE3MTMxOTl9.Q7EeZhLJJXKe8BJNw0-Ob0blomLaxit87FNn5eL8N6k'
            }else {
                //header.Authorization = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOjEzMDEzNDAzLCJpZCI6NjQ2ODY4OSwidGltZSI6MTcwMTI2NjEzNCwiaWF0IjoxNzAxMjY2MTM0LCJleHAiOjE3MDQ4NjYxMzR9.yuIFXL2X5MTvlH3Na87OHp9e5t7fjYMpFiVC9NDQWBo'
            }

            let response = await Taro.uploadFile({
                url : process.env.B_API_URL + '/' + url,
                header,
                name : file.name,
                filePath : file.path,
                formData
            })
            if(response.data) {
                const data = JSON.parse(response.data)
                result.code = data.code
                result.data = data.data
                result.msg = data.msg
                result.success = data.success
            }
            
        } catch (error) {
            console.log(error)
        }

        return result
    }

    static async send<T = any>(url: string, options: Options) : Promise<ApiResult<T>>{
        const global = useGlobalStore()


        let result:ApiResult<T> = {
            msg : '',
            success : false
        }


        try {
            let header:{[key : string] : string | number} = {
                'Content-type' : 'application/json',
                'client-type' : global.client
            }

            if(global.accessToken) {
                header.Authorization = global.accessToken//'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOjEzMDEzNDAzLCJpZCI6NjQ1NjcwMSwidGltZSI6MTY5ODExMzE5OSwiaWF0IjoxNjk4MTEzMTk5LCJleHAiOjE3MDE3MTMxOTl9.Q7EeZhLJJXKe8BJNw0-Ob0blomLaxit87FNn5eL8N6k'
            }else {
                //header.Authorization = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOjEzMDEzNDAzLCJpZCI6NjQ2ODY4OSwidGltZSI6MTcwMTI2NjEzNCwiaWF0IjoxNzAxMjY2MTM0LCJleHAiOjE3MDQ4NjYxMzR9.yuIFXL2X5MTvlH3Na87OHp9e5t7fjYMpFiVC9NDQWBo'
            }
            
            let response = await Taro.request({
                url : process.env.B_API_URL + '/' + url,
                ...options,
                header,
                dataType : 'json'
            })

            let data = response.data
            if(data) {
                result.code = data.code
                result.data = data.data
                result.msg = data.msg
                result.success = data.success
            }
            
        } catch (error) {
            console.log(error)
        }

        return result
    }

}

export default Http